﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using DocumentFormat.OpenXml.Spreadsheet;
using ExtremeML.Packaging;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Spreadsheet
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class HyperlinkWrapperCollection
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing <see cref="WorksheetPartWrapper"/>.
		/// </summary>
		public WorksheetPartWrapper WorksheetPart
		{
			get { return (WorksheetPartWrapper)Worksheet.Parent; }
		}

		/// <summary>
		/// Gets the containing <see cref="WorksheetWrapper"/>.
		/// </summary>
		public WorksheetWrapper Worksheet
		{
			get { return (WorksheetWrapper)Parent.Parent; }
		}

		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns a <see cref="HyperlinkWrapper"/> that wraps a new <see cref="Hyperlink"/>.
		/// </summary>
		/// <param name="uri">A <see cref="Uri"/> that represents the uri targeted by the hyperlink.</param>
		/// <param name="address">A <see cref="CellAddress"/> that represents the cell associated with the hyperlink.</param>
		/// <returns>A new instance of <see cref="HyperlinkWrapper"/>.</returns>
		internal HyperlinkWrapper Add(
			Uri uri,
			CellAddress address)
		{
			var result = Add();
			result.CellAddress.RowIndex = address.RowIndex;
			result.CellAddress.ColumnIndex = address.ColumnIndex;
			result.CellAddress.RowAbsolute = false;
			result.CellAddress.ColumnAbsolute = false;
			result.Id = WorksheetPart.Hyperlinks.Add(uri).Id;
			result.Activate();
			return result;
		}

		#endregion
		#region Overrides

		/// <summary>
		/// Performs housekeeping and validation of a requested deletion.
		/// </summary>
		protected override void OnCustomRemoved(
			HyperlinkWrapper element)
		{
			if (Count == 0)
			{
				// Since the schema prohibits empty Hyperlinks elements, deletion
				// of the last Hyperlink element requires deletion of its container.
				((HyperlinksWrapper)Parent).Remove();
			}
		}

		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods


		#endregion
	}
}
